//
//  DUImageButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/11/10.
//

import Cocoa

class DUImageButton: BaseButton {

    private var btnImageLayer:CALayer!
    private var mouseImageLayer:CALayer!
    private var mouseInButton = false
    
    //private var btnImage:NSImage?
    //private var mouseImage:NSImage?
    
    init(frame frameRect: NSRect, btnImage:NSImage, mouseImage: NSImage) {
        super.init(frame: frameRect)
        self.btnImage = btnImage
        self.mouseImage = mouseImage
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func viewDidMoveToWindow() {
        let btn = createImage(img: btnImage)
        btnImageLayer = CALayer()
        btnImageLayer?.contents = btn
        btnImageLayer?.frame = getFrame()//self.frame
        btnImageLayer?.isHidden = false
        
        let mbtn = createImage(img: mouseImage)
        mouseImageLayer = CALayer()
        mouseImageLayer?.contents = mbtn
        mouseImageLayer?.frame = getFrame()//self.frame
        mouseImageLayer?.isHidden = true
        self.allowsMixedState = false
       // self.action =
        addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false)
    }
    
    override func layout() {
        if self.layer?.sublayers != nil {
            return
        }
        self.layer?.addSublayer(btnImageLayer)
        self.layer?.addSublayer(mouseImageLayer)
    }
    
    override var wantsUpdateLayer: Bool {
        return true
    }
    
    override func mouseEntered(with event: NSEvent) {
        //self.state = .on
        self.mouseInButton = true
        self.needsDisplay = true
    }
    
    override func mouseExited(with event: NSEvent) {
        self.mouseInButton = false
        self.needsDisplay = true
    }
    
    override func updateLayer() {
        if self.isEnabled {
            return
        }
        if self.mouseInButton {
            self.btnImageLayer?.isHidden = true
            self.mouseImageLayer?.isHidden = false
        } else {
            self.btnImageLayer?.isHidden = false
            self.mouseImageLayer?.isHidden = true
        }
    }
    
    func setFixedConstraint (width: CGFloat, height:CGFloat) {
        let fixedWidth = NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: width)
        let fixedHeight = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: height)
        self.addConstraints([fixedWidth, fixedHeight])
        
    }
    
    private func createImage(img:NSImage?) -> NSImage {
        let dsImage = NSImage(size: getSize())
        dsImage.lockFocus()
        img?.draw(in: NSMakeRect(0, 0, dsImage.size.width, dsImage.size.height))
        dsImage.unlockFocus()
        return dsImage
    }
    
    private func getFrame () -> NSRect {
        let s = getSize()
        let r = NSMakeRect(2, 2, s.width, s.height)
        return r
    }
    
    private func getSize() -> CGSize {
        let size = CGSize(width: self.frame.size.width - 4, height: self.frame.size.height - 4)
        return size
    }
    
    @IBInspectable var btnImage:NSImage? {
        didSet{
            if btnImageLayer != nil {
                btnImageLayer.contents = btnImage
            }
        }
    }
    
    @IBInspectable var mouseImage:NSImage? {
        didSet {
            if mouseImageLayer != nil {
                mouseImageLayer.contents = mouseImage
            }
        }
    }
}
